/**
 * Created with IntelliJ IDEA.
 * Description: leetcode: 868. 二进制间距
 * <a href="https://leetcode.cn/problems/binary-gap/">...</a>
 * User: DELL
 * Date: 2023-10-12
 * Time: 9:37
 */
public class Solution {
    /**
     * 解题思路: (按位运算)
     * 本题较为简单,只需要进行按位判断,同时使用两个标志变量记录即可.
     *
     * @param n
     * @return
     */
    public int binaryGap(int n) {
        int temp = -1;
        int max = 0;
        for (int i = 0; i < 32; i++) {
            if ((n & (1 << i)) != 0) {
                if (temp == -1) {
                    temp = i;
                } else {
                    max = Math.max(max, i - temp);
                    temp = i;
                }
            }
        }
        return max;
    }
}